<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-input
        v-model="listQuery.product_name"
        placeholder="请输入拍品名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.user"
        placeholder="请输入账户"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="请输入姓名"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="dateInput"
        class="filter-item"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00', '23:59:59']"
        style="max-width: 100%; margin-left: 10px"
        @change="dateInputHandle"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        重置
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="出价人账户" prop="user.user" align="center" />
      <el-table-column label="姓名" align="center" prop="user.name" />
      <el-table-column label="拍品" header-align="center">
        <template slot-scope="{ row }">
          <el-image
            v-if="row.product"
            style="width: 40px; height: 40px; text-align: center"
            :src="$store.state.settings.imgHost + row.product.image"
            :preview-src-list="[
              $store.state.settings.imgHost + row.product.image,
            ]"
          />
          <span
            v-if="row.product"
            style="margin-left: 10px; vertical-align: top"
          >{{ row.product.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="出价价格" prop="money" />
      <el-table-column align="center" label="出价状态">
        <template slot-scope="{ row }">{{
          row.status == 0 ? "领先" : row.status == 1 ? "出局" : ""
        }}</template>
      </el-table-column>
      <el-table-column align="center" label="出价时间" prop="created_at" />
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getBidrecord } from '@/api/common'
import { utcAdd } from '@/utils'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'AuctioningBidRecord',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      dateInput: undefined,
      listQuery: {
        page: 1,
        per_page: 20,
        start_time: undefined,
        end_time: undefined,
        product_name: undefined,
        user: undefined,
        name: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取时间
    dateInputHandle(e) {
      if (e && e.length > 0) {
        this.listQuery.start_time = utcAdd(e[0])
        this.listQuery.end_time = utcAdd(e[1])
      } else {
        this.listQuery.start_time = undefined
        this.listQuery.end_time = undefined
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['start_time'] = undefined
      this.listQuery['end_time'] = undefined
      this.listQuery['product_name'] = undefined
      this.listQuery['user'] = undefined
      this.listQuery['name'] = undefined
      this.dateInput = undefined
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getBidrecord(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
